<?php
        
        include "../database.php"; 

        $fileString = "";
        $file = $_FILES["file"]["tmp_name"];
        $project_id = $_POST['doc_project_id'];
        
        echo $file;
        echo $project_id;

        if (file_exists($_FILES["file"]["tmp_name"])){
                if ($_FILES["file"]["type"] == "text/plain"){

                $fh = fopen($_FILES["file"]["tmp_name"], 'r');

                if ($fh != FALSE){

                    while(!feof($fh)){
                        $fileString = $fileString.fgets($fh);
                        //echo fgets($fh);
                    }

                    fclose($fh);   

                    $queryFindDoc = "SELECT * FROM documentation WHERE documentation_project_id = '$project_id'";
                    $resultFindDoc = executeQuery($queryFindDoc);
                    $num_rows = mysql_num_rows($resultFindDoc);

                    if ($num_rows < 1){
                        $query = "INSERT INTO documentation (documentation_project_id, documentation_content) VALUES ('$project_id', '$fileString')";
                        executeQuery($query);
                    }else{

                        $queryGetContent = "SELECT documentation_content FROM documentation WHERE documentation_project_id = '$project_id'";
                        $doc_content = mysql_fetch_array(executeQuery($queryGetContent))[0];

                        $doc_content = $doc_content.$fileString;
                        //$doc_content = ($doc_content);

                        $query = "UPDATE documentation SET documentation_content = '$doc_content' WHERE documentation_project_id = '$project_id'";
                        executeQuery($query);
                    } 
                    header("Location: ../project_wall.php?tab=2&imp_error=0");
                    return;
                }
                header("Location: ../project_wall.php?tab=2&imp_error=1");
                return;
            }
            header("Location: ../project_wall.php?tab=2&imp_error=2");
            return;
        }
        header("Location: ../project_wall.php?tab=2&imp_error=3");
        return;
?>
